<template>
	<view class="review-page">
		<!-- 商品信息 -->
		<view class="review-top-row">
			<view class="review-product-img">
				<image src="/static/svg/review-img-placeholder.svg" mode="aspectFit" style="width:50px;height:50px;" />
			</view>
			<view class="review-rate-row">
				<view v-for="(item, idx) in rateList" :key="item.label" :class="['review-rate-item', selectedRate === idx ? 'active' : '']" @click="chooseRate(idx)">
					<image :src="item.icon" mode="aspectFit" style="width:32px;height:32px;" />
					<text class="review-rate-text">{{item.label}}</text>
				</view>
			</view>
		</view>

		<!-- 评价输入框 -->
		<view class="review-input-row">
			<textarea class="review-input" placeholder="和大家分享这份美好吧~" v-model="commentText" @input="handleInput" />
			<view class="review-word-count">{{currentWordCount}}/{{maxWordCount}}</view>
		</view>

		<!-- 图片上传和提示语 -->
		<view class="review-img-tip-row">
			<view class="review-upload-list">
				<view v-for="(img, idx) in imageList" :key="img" class="review-upload-img-box">
					<image :src="img" mode="aspectFill" class="review-upload-img" />
					<view class="review-upload-del" @click="removeImage(idx)">
						<image src="/static/svg/delete-icon.svg" mode="aspectFit" style="width:32rpx;height:32rpx;" />
					</view>
				</view>
				<view v-if="imageList.length < 9" class="review-upload-box" @click="chooseImage">
					<image src="/static/svg/review-upload-plus.svg" mode="aspectFit" style="width:60rpx;height:60rpx;" />
					<view class="review-upload-text">添加图片</view>
				</view>
			</view>
		</view>
		<view class="review-tip">你的评价能帮助其他小伙伴哦</view>

		<!-- 匿名 -->
		<view class="review-anonymous-row">
			<view class="review-checkbox">
				<image src="/static/svg/review-checkbox.svg" mode="aspectFit" style="width:22px;height:22px;" />
			</view>
			<text class="review-anonymous-text">匿名</text>
		</view>

		<!-- 提交按钮 -->
		<view class="review-submit-row">
			<button class="review-submit-btn">提交</button>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';

// 图片列表
const imageList = ref([]);

// 评价类型 0-好评 1-中评 2-差评
const selectedRate = ref(0);
const rateList = [
	{ label: '好评', icon: '/static/svg/review-rate-good.svg' },
	{ label: '中评', icon: '/static/svg/review-rate-normal.svg' },
	{ label: '差评', icon: '/static/svg/review-rate-bad.svg' }
];

const chooseRate = (idx) => {
	selectedRate.value = idx;
};

// 选择图片
const chooseImage = () => {
	uni.chooseImage({
		count: 9 - imageList.value.length,
		success: (res) => {
			imageList.value = imageList.value.concat(res.tempFilePaths);
		}
	});
};

// 删除图片
const removeImage = (idx) => {
	imageList.value.splice(idx, 1);
};

// 评价文字相关
const maxWordCount = 200; // 最大字数限制
const currentWordCount = ref(0); // 当前字数
const commentText = ref(''); // 评价内容

// 处理输入
const handleInput = (e) => {
	const text = e.detail.value;
	if (text.length > maxWordCount) {
		commentText.value = text.slice(0, maxWordCount);
	}
	currentWordCount.value = commentText.value.length;
};
</script>

<style scoped>
.review-page {
	background: #fff;
	min-height: 100vh;
	padding-bottom: 120rpx;
}
.review-top-row {
	display: flex;
	align-items: center;
	padding: 32rpx 0 0 32rpx;
}
.review-product-img {
	width: 100rpx;
	height: 100rpx;
	border-radius: 12rpx;
	overflow: hidden;
	margin-right: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background: #f6f6f6;
}
.review-rate-row {
	display: flex;
	align-items: center;
}
.review-rate-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	margin-right: 64rpx;
	cursor: pointer;
}
.review-rate-item:last-child {
	margin-right: 0;
}
.review-rate-item.active .review-rate-text {
	color: #ff6a23;
	font-weight: bold;
}
.review-rate-item.active image {
	filter: none;
}
.review-rate-text {
	font-size: 28rpx;
	color: #bbb;
	margin-top: 8rpx;
}
.review-input-row {
	margin: 48rpx 0 0 32rpx;
	position: relative;
}
.review-input {
	width: 90vw;
	min-height: 160rpx;
	background: #f7f7f7;
	border-radius: 12rpx;
	border: none;
	padding: 28rpx;
	font-size: 28rpx;
	color: #333;
	resize: none;
}
.review-word-count {
	position: absolute;
	right: 32rpx;
	bottom: 16rpx;
	font-size: 24rpx;
	color: #999;
}
.review-img-tip-row {
	display: flex;
	flex-direction: row;
	align-items: flex-end;
	margin: 48rpx 0 0 32rpx;
}
.review-upload-list {
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	gap: 20rpx;
	align-items: flex-end;
}
.review-upload-img-box {
	position: relative;
	width: 160rpx;
	height: 160rpx;
	border-radius: 10rpx;
	overflow: hidden;
	margin-right: 0;
	margin-bottom: 0;
}
.review-upload-img {
	width: 100%;
	height: 100%;
	border-radius: 10rpx;
	object-fit: cover;
}
.review-upload-del {
	position: absolute;
	top: 0;
	right: 0;
	width: 40rpx;
	height: 40rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background: rgba(0,0,0,0.4);
	border-bottom-left-radius: 10rpx;
}
.review-upload-box {
	width: 160rpx;
	height: 160rpx;
	border-radius: 10rpx;
	border: 1px dashed #ddd;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background: #fafafa;
	margin-right: 32rpx;
}
.review-upload-text {
	color: #bbb;
	font-size: 26rpx;
	margin-top: 8rpx;
}
.review-tip {
	color: #bbb;
	font-size: 24rpx;
	margin: 24rpx 0 0 32rpx;
}
.review-anonymous-row {
	display: flex;
	align-items: center;
	margin: 48rpx 0 0 32rpx;
}
.review-checkbox {
	margin-right: 16rpx;
}
.review-anonymous-text {
	color: #333;
	font-size: 28rpx;
}
.review-submit-row {
	position: fixed;
	left: 0;
	bottom: 0;
	width: 100vw;
	background: transparent;
	display: flex;
	justify-content: center;
	align-items: center;
	z-index: 10;
}
.review-submit-btn {
	width: 92vw;
	height: 100rpx;
	background: #ff6a23;
	color: #fff;
	font-size: 36rpx;
	border: none;
	border-radius: 50rpx;
	margin-bottom: 16rpx;
	font-weight: bold;
	letter-spacing: 4rpx;
}
</style>
